/**
 * 菜单JS
 */
angular.module('mall.search', [])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
        //搜索功能页
            .state('search', {
                url: '/search',
                templateUrl: templateprefix + 'ngb/menu/search.html',
                resolve: {
                    VillageList: ['ShopService', function (ShopService) {
                        return ShopService.selectAllCountyShops(330329);
                    }]
                },
                controller: 'SearchController'
            })
        ;
    }])

    .controller('SearchController', ['$scope', '$state', 'GoodsService', 'ShopService', 'VillageList',
        function ($scope, $state, GoodsService, ShopService, VillageList) {
            $scope.villagelist = VillageList;
            $scope.shops = [];
            for (var i = 0; i < $scope.villagelist.length; i++) {
                $scope.villages = $scope.villagelist[i].shops;
                for (var j = 0; j < $scope.villages.length; j++) {
                    $scope.shop = $scope.villages[j];
                    $scope.shops.push($scope.shop)
                }
            }
            //获取所有商品
            $scope.inner = function (shopid) {//页面跳转
                //$rootScope.shopid = shopid;
                $state.go("goods", {shopid: shopid});
                //$state.go("model.goods");
            }
            focus()
            $scope.search = function () {
                var len = $scope.content.name.length;
                search_style(len);
            }
            $scope.cleansearch = function () {
                $scope.content.name = null
                hide_icon()
                focus()
            }
            $scope.Change = 0;
            $(".products").hide()
            $scope.change_x = function (data) {
                $scope.Change = data.Change
                if ($scope.Change == 0) {
                    $(".products").hide()
                    $(".markets").show()
                } else if ($scope.Change == 1) {
                    $(".markets").hide()
                    $(".products").show()
                }
            }
            $scope.searchbtn = function () {
                if ($scope.content.name && $scope.Change == 1) {
                    $scope.goodslist = GoodsService.queryGoodsListByKeyword($scope.content.name);
                    $scope.goodslist.$promise.then(function (goodslist) {
                        for (var i = 0; i < goodslist.length; i++) {
                            var goods = goodslist[i];
                            goods.shopname = ShopService.queryShopByShopid(goods.shopid).name;
                        }
                    });
                }
            }
        }])
;
